<?php


namespace app\controller\admin\statistics;


use app\BaseController;
use app\model\coach\CoachMoneyDetailModel;
use app\model\coach\UserCoachModel;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\UserPartnerModel;
use app\model\user\PartnerMoneyRecordModel;
use app\model\user\UserModel;
use app\model\user\UserMoneyDetailModel;
use app\service\user\PartnerMoneyRecordService;

class VerifyCarSellController extends BaseController
{
    public function getList()
    {
        try {
            $where = [];
            $where[] = ['status', 'in', [1, 3, 5, 7, 8]];
            $mobile = $this->request->param('mobile', '', 'trim');
            if ($mobile) {
                $where[] = ['mobile', 'like', "%{$mobile}%"];
            }

            $orderBy = $this->request->param('order_by', 'id', 'trim');
            $sort = $this->request->param('sort', 'desc', 'trim');
            $list = EscortMenuReserveOrderModel::with(['menuOrder', 'city'])
                ->where($where)
                ->field('order_sn,order_id,city_id,create_time,user_id')
                ->order([$orderBy => $sort])
                ->paginate(['list_rows' => $this->limit, 'page' => $this->page])
                ->toArray();
            foreach ($list['data'] as $k => $v) {
                // 分享人
                $userInfo = UserModel::withTrashed()->find($v['user_id']);
                $list['data'][$k]['user_mobile'] = $userInfo['mobile'];
                if ($userInfo['share_type'] == 0) {
                    $shareName = UserModel::where('id', $userInfo['pid'])->value('username');
                    $shareAmount = UserMoneyDetailModel::where('order_sn', $v['order_sn'])->where('type', 1)->value('amount');
                } elseif ($userInfo['share_type'] == 1) {
                    $shareName = UserCoachModel::where('id', $userInfo['coach_id'])->value('username');
                    $shareAmount = CoachMoneyDetailModel::where('order_sn', $v['order_sn'])->where('type', 1)->value('amount');
                } else {
                    $shareName = UserPartnerModel::where('id', $userInfo['partner_id'])->value('username');
                    $shareAmount = PartnerMoneyRecordModel::where('order_sn', $v['order_sn'])->where('type', 4)->value('amount');
                }
                $list['data'][$k]['share_name'] = $shareName;
                // 分享奖励
                $list['data'][$k]['share_amount'] = $shareAmount;
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception);
        }
    }
}